//
//  KKFileManager.m
//  Questionnaire
//
//  Created by leo on 16/1/26.
//  Copyright © 2016年 leo. All rights reserved.
//

#import "KKFileManager.h"

@interface KKFileManager() {
    
    NSArray *_kk_unFinishedQueestionnaireItems;
}

@end

@implementation KKFileManager

+ (instancetype)defaultManager {
    static KKFileManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [KKFileManager new];
    });
    return manager;
}

#pragma mark - Public methods

/**
 *  未完成的问卷
 *
 *  @return 问卷数组
 */
- (NSArray *)unFinishedQuestionnaireItemsFromCacheFile {
    
    // 1.获得Documents的全路径
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 2.获得文件的全路径
    NSString *path = [doc stringByAppendingPathComponent:@"unfinished.data"];
    
    _kk_unFinishedQueestionnaireItems = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return _kk_unFinishedQueestionnaireItems;
}

/**
 *  保存未完成的问卷
 *
 *  @param items 问卷对象
 */
- (void)saveUnFinishedQuestionnaireItemsToCacheFile:(NSArray *)items {
    // 1.获得Documents的全路径
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 2.获得文件的全路径
    NSString *path = [doc stringByAppendingPathComponent:@"unfinished.data"];
    
    [NSKeyedArchiver archiveRootObject: items toFile: path];
}

@end
